home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / begincpp.zip / #6.CPP < prev    next >
C/C++ Source or Header  |  1990-08-04  |  3KB  |  108 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. enum year { freshman, sophomore, junior, senior };
  5. enum support { ta, ra, fellowship, other };
  6. enum level { undergraduate, graduate };
  7.  
  8. class student {
  9.  
  10.   private:               //private member only visible in base class
  11.      level pr_member;    //equivalent to int pr_member;
  12.      void set_pr_member() { pr_member = undergraduate; }  //private function
  13.  
  14.   protected:             //visible to subclasses derived from student
  15.      int student_id;     
  16.      float gpa;
  17.  
  18.   public:
  19.      student (int id, float g) { set_pr_member(); student_id = id; gpa = g; }
  20.      char name [15];
  21.      char college [12];
  22.      char major [14];
  23.      year yr;
  24.      void print() { printf("%-15s %-15s %4d %-12s %-14s %-9s %4.2f\n", 
  25.                     l_char(pr_member), name, student_id, college, 
  26.                     major, y_char(yr), gpa); 
  27.      }
  28.      friend char * y_char (year);    //convert year to characters
  29.      friend char * l_char (level);    //convert level to characters
  30. };
  31.  
  32. class grad_student : public student {
  33.   private:
  34.      support s;
  35.  
  36.   public:
  37.      grad_student(support x, int id, float g) : (id, g) { s = x; }
  38.      char dept [15];
  39.      char thesis [50];
  40.      void print() { printf("%-15s%-10s %-50s\n", dept, s_char(s), thesis); }
  41.  
  42.      friend char *s_char(support);    //convert support to characters
  43. };
  44.  
  45. void proc1(student &), proc2(grad_student &);     //pass by reference
  46.  
  47. main() 
  48. {
  49.      student s1 (100, 2.99);
  50.      grad_student gs1 (ta, 200, 3.01);
  51.  
  52.      proc1(s1);
  53.      s1.print();
  54.  
  55.      proc2(gs1);
  56.      gs1.print();
  57. }
  58.  
  59. void proc1(student &sp)            //assign to sp
  60.      strcpy(sp.name,    "Loope deLoop");
  61.      strcpy(sp.college, "Art&Science"); 
  62.      strcpy(sp.major,   "French"); 
  63.      sp.yr = senior;
  64. }
  65.  
  66. void proc2(grad_student &gsp)      //assign to gsp
  67. {
  68.      strcpy(gsp.name, "Yogi Bear");
  69.      strcpy(gsp.dept, "Entertainment"); 
  70.      strcpy(gsp.college, "Business");
  71.      strcpy(gsp.thesis, "10000 ways to be smarter than the average bears");
  72.      strcpy(gsp.major, ""); 
  73. }
  74.  
  75. char * l_char (level l)  
  76. {
  77.      switch (l) {
  78.           case undergraduate : return "Undergraduate";
  79.           case graduate      : return "Graduate";
  80.      }
  81. }
  82.  
  83. char * y_char (year yr)  
  84. {
  85.      switch (yr) {
  86.           case freshman : return "Freshman";
  87.           case sophomore: return "Sophomore";
  88.           case junior   : return "Junior";
  89.           case senior   : return "Senior";
  90.      }
  91. }
  92.  
  93. char * s_char (support s)
  94. {
  95.      switch (s) {
  96.           case ta : return "Ta";
  97.           case ra : return "Ra";
  98.           case fellowship : return "Fellowship";
  99.           case other : return "Other";
  100.      }
  101. }
  102. /* output:
  103.  
  104. Undergraduate   Loope deLoop     100 Art&Science  French         Senior    2.99
  105. Entertainment  Ta         10000 ways to be smarter than the average bears   
  106.  
  107. */